package cn.community.system.service.impl;

import cn.community.system.domain.Admin;
import cn.community.system.exception.LogicException;
import cn.community.system.mapper.AdminMapper;
import cn.community.system.mapper.RoleMapper;
import cn.community.system.query.QueryObject;
import cn.community.system.service.IAdminService;
import cn.community.system.util.AssertUtils;
import cn.community.system.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void delete(Long id) {
        adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Admin admin) {
        // 加密
        String password = admin.getPassword();
        Md5Hash md5Hash = new Md5Hash(password, admin.getUsername());
        admin.setPassword(md5Hash.toString());
        if (admin.getImgUrl() == ""){
            admin.setImgUrl(null);
        }
        adminMapper.insert(admin);
    }

    @Override
    public Admin get(Long id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Admin> listAll() {
        return adminMapper.selectAll();
    }

    @Override
    public void update(Admin admin) {
        // 参数校验
        String username = admin.getUsername();
        String password = admin.getPassword();
        AssertUtils.hasLength(admin.getNumber(),"编号不能为空！");
        AssertUtils.hasLength(username,"用户名不能为空！");
        AssertUtils.hasLength(password,"密码不能为空！");
        // 密码加密
        Admin currentAdmin = UserContext.getCurrentAdmin();
        if(!currentAdmin.getPassword().equals(password)){ // 密码改变时才加密
            Md5Hash md5Hash = new Md5Hash(password, username);
            admin.setPassword(md5Hash.toString());
        }
        // 图片
        if(admin.getImgUrl() == ""){
            admin.setImgUrl(null);
        }
        adminMapper.updateByPrimaryKey(admin);
    }

    public PageInfo query(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询结果集
        List<Admin> list = adminMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Admin userLogin(String userName, String password) {
        Admin admin = adminMapper.selectByNameAndPassword(userName, password);
        System.out.println(admin.toString());
        if (admin == null){
            throw new LogicException("账号或密码错误！");
        }
        return admin;
    }

    @Override
    public void batchDelete(Long[] ids) {
        adminMapper.deleteBatch(ids);
    }

    @Override
    public Admin getAdminByUsername(String username) {
        return adminMapper.selectByName(username);
    }

}
